package com.wy.jfree_chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import javax.swing.*;
import java.awt.*;

public class ExamReportDashboard extends JFrame {

    public ExamReportDashboard() {
        super("考试报告");
        initializeUI();
    }

    private void initializeUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        
        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 标题
        JLabel titleLabel = new JLabel("考试成绩报告", JLabel.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(titleLabel);
        
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 学生信息面板
        JPanel infoPanel = createInfoPanel();
        mainPanel.add(infoPanel);
        
        mainPanel.add(Box.createRigidArea(new Dimension(0, 30)));

        // 图表面板
        JPanel chartPanel = createChartPanel();
        mainPanel.add(chartPanel);

        JScrollPane scrollPane = new JScrollPane(mainPanel);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getViewport().setBackground(Color.WHITE);
        
        add(scrollPane, BorderLayout.CENTER);
        
        setSize(900, 700);
        setLocationRelativeTo(null);
    }

    private JPanel createInfoPanel() {
        JPanel panel = new JPanel(new GridLayout(2, 3, 15, 10));
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder("学生信息"));

        // 信息项
        addInfoItem(panel, "姓名:", "张三");
        addInfoItem(panel, "学号:", "20230001");
        addInfoItem(panel, "班级:", "高三(1)班");
        addInfoItem(panel, "考试时间:", "2024-01-15");
        addInfoItem(panel, "总分:", "485");
        addInfoItem(panel, "年级排名:", "25/300");

        return panel;
    }

    private void addInfoItem(JPanel panel, String label, String value) {
        JPanel itemPanel = new JPanel(new BorderLayout());
        itemPanel.setBackground(Color.WHITE);
        
        JLabel labelComp = new JLabel(label);
        labelComp.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
        
        JLabel valueComp = new JLabel(value);
        valueComp.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
        
        itemPanel.add(labelComp, BorderLayout.WEST);
        itemPanel.add(valueComp, BorderLayout.CENTER);
        
        panel.add(itemPanel);
    }

    private JPanel createChartPanel() {
        JPanel panel = new JPanel(new GridLayout(1, 2, 20, 0));
        panel.setBackground(Color.WHITE);

        // 环形图
        JFreeChart donutChart = createEnhancedDonutChart();
        ChartPanel donutPanel = new ChartPanel(donutChart);
        donutPanel.setPreferredSize(new Dimension(300, 300));
        donutPanel.setBackground(Color.WHITE);
        
        JPanel donutContainer = new JPanel(new BorderLayout());
        donutContainer.setBackground(Color.WHITE);
        donutContainer.setBorder(BorderFactory.createTitledBorder("成绩分布"));
        donutContainer.add(donutPanel, BorderLayout.CENTER);
        
        // 柱状图
        JFreeChart barChart = createEnhancedBarChart();
        ChartPanel barPanel = new ChartPanel(barChart);
        barPanel.setPreferredSize(new Dimension(400, 300));
        barPanel.setBackground(Color.WHITE);
        
        JPanel barContainer = new JPanel(new BorderLayout());
        barContainer.setBackground(Color.WHITE);
        barContainer.setBorder(BorderFactory.createTitledBorder("各科成绩"));
        barContainer.add(barPanel, BorderLayout.CENTER);

        panel.add(donutContainer);
        panel.add(barContainer);

        return panel;
    }

    private JFreeChart createEnhancedDonutChart() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("优秀\n90-100", 25);
        dataset.setValue("良好\n80-89", 35);
        dataset.setValue("中等\n70-79", 20);
        dataset.setValue("及格\n60-69", 15);
        dataset.setValue("不及格\n0-59", 5);

        JFreeChart chart = ChartFactory.createPieChart("", dataset, false, true, false);
        
        PiePlot plot = (PiePlot) chart.getPlot();
//        plot.setSectionDepth(0.35);
        plot.setCircular(true);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setOutlineVisible(false);
        plot.setShadowPaint(null);
        
        // 设置颜色
        plot.setSectionPaint("优秀\n90-100", new Color(46, 117, 182));
        plot.setSectionPaint("良好\n80-89", new Color(86, 156, 214));
        plot.setSectionPaint("中等\n70-79", new Color(126, 185, 234));
        plot.setSectionPaint("及格\n60-69", new Color(166, 206, 237));
        plot.setSectionPaint("不及格\n0-59", new Color(206, 226, 247));
        
        // 设置标签
        plot.setLabelFont(new Font("Microsoft YaHei", Font.PLAIN, 10));
        plot.setLabelBackgroundPaint(Color.WHITE);
        plot.setLabelShadowPaint(null);
        plot.setLabelOutlinePaint(null);
        plot.setLabelGenerator(new org.jfree.chart.labels.StandardPieSectionLabelGenerator("{0}: {1}%"));

        return chart;
    }

    private JFreeChart createEnhancedBarChart() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        String[] subjects = {"语文", "数学", "英语", "物理", "化学", "生物"};
        int[] scores = {85, 92, 78, 88, 76, 90};
        int[] avgScores = {82, 85, 80, 75, 78, 83};
        
        for (int i = 0; i < subjects.length; i++) {
            dataset.addValue(scores[i], "我的分数", subjects[i]);
            dataset.addValue(avgScores[i], "平均分", subjects[i]);
        }

        JFreeChart chart = ChartFactory.createBarChart("", "", "分数", dataset);
        
        CategoryPlot plot = chart.getCategoryPlot();
        
        // 设置渲染器
        BarRenderer renderer = new BarRenderer();
        renderer.setSeriesPaint(0, new Color(46, 117, 182)); // 我的分数 - 蓝色
        renderer.setSeriesPaint(1, new Color(200, 200, 200)); // 平均分 - 灰色
        renderer.setShadowVisible(false);
        renderer.setDrawBarOutline(false);
        
        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinePaint(new Color(240, 240, 240));
        plot.setOutlineVisible(false);
        
        // 设置坐标轴
        plot.getDomainAxis().setTickLabelFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
        plot.getRangeAxis().setTickLabelFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
        
        // 添加图例
        chart.getLegend().setItemFont(new Font("Microsoft YaHei", Font.PLAIN, 11));

        return chart;
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getLookAndFeel());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        SwingUtilities.invokeLater(() -> {
            new ExamReportDashboard().setVisible(true);
        });
    }
}